package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    //回显父菜单和菜单Id
    @RequestMapping("/findMenuparentMenuId")
    public Result findMenuparentMenuId(){
        try {
            List<Map> list = menuService.findMenuparentMenuId();
            return new Result(true,"查询数据成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败");
        }
    }

    //新增菜单
    @RequestMapping("/add")
    public Result add(Integer id,@RequestBody Menu menu){
        try {
            menuService.add(id,menu);
            return new Result(true,"新增菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增菜单失败");
        }
    }

    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = menuService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }

    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }

    //回显选中的父菜单和父菜单的父菜单的id
    @RequestMapping("/findParentIdsByMenuId")
    public List findParentIdsByMenuId(Integer mid){
        List<Integer> list= menuService.findParentIdsByMenuId(mid);
        return list;
    }

    //findMenuIdAndDisabled 根据id查询选择数据
    @RequestMapping("/findMenuIdAndDisabled")
    public Result findMenuIdAndDisabled(Integer id){
        try {
            List<Map> list = menuService.findMenuIdAndDisabled(id);
            return new Result(true,"查询数据成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询数据失败");
        }
    }

    //delete
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            menuService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false,"删除菜单失败");
        }
        return new Result(true,"删除菜单成功");
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,"编辑菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单失败");
        }

    }

}
